//
//  ViewController.swift
//  testSnap
//
//  Created by czh on 2016/10/11.
//  Copyright © 2016年 zwl. All rights reserved.
//

import UIKit
import SnapKit
import MJRefresh
import MBProgressHUD

private let reuseID = "reuseID"
private let KHeaderViewH : CGFloat = 250

class ViewController: UIViewController {
    
    let header = MJRefreshNormalHeader()
    
    
    
    // MARK:- 懒加载控件
    fileprivate lazy var mytableView : UITableView = {
        let mytableView = UITableView(frame: self.view.bounds)
        mytableView.backgroundColor = UIColor.lightGray
        mytableView.dataSource = self
        mytableView.separatorStyle = .none
        mytableView.register(ZWLMyCell.self, forCellReuseIdentifier: reuseID)
        
        return mytableView
    }()


    // MARK:- 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        setupRefrsh()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
    }

}

// MARK:- tableView 的数据源和代理方法
extension ViewController : UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseID) as! ZWLMyCell
        
        return cell
        
    }
}

// 设置刷新控件
extension ViewController {
    fileprivate func setupRefrsh() {
        
        header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        mytableView.mj_header = header
    }
    
    
    
    @objc fileprivate func headerRefresh() {
        print("1")
        
        // 延时执行
        let when = DispatchTime.now() + 3
        DispatchQueue.main.asyncAfter(deadline: when) {
            self.mytableView.mj_header.endRefreshing()
            
        }
        
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        hud.label.text = "加载中..."
//        hud.mode = MBProgressHUDMode.text
//        hud.customView = UIImageView(image: UIImage(named: "test"))
        hud.hide(animated: true, afterDelay: 3.0)
 
      
    }
}

// MARK:- 设置 UI
extension ViewController {
    fileprivate func setupUI() {

        // 导航条设置
        view.backgroundColor = UIColor.black
        navigationController?.navigationBar.barTintColor = UIColor.black
        var dict = [String : Any]()
        dict[NSForegroundColorAttributeName] = UIColor.white
        dict[NSFontAttributeName] = UIFont.systemFont(ofSize: 18)
        navigationController?.navigationBar.titleTextAttributes = dict
        navigationItem.title = "朋友圈"
        
        // 添加 tableView
        view.addSubview(self.mytableView)
        
        // 头部视图
        let headerViewF = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: KHeaderViewH)
        mytableView.tableHeaderView = ZWLHeaderView(frame: headerViewF)
        
    }
}

